//
//  MessageService.m
//  Conference
//
//  Created by Xiang He on 12-12-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MessageService.h"
#import "Globals.h"
#import "WebData.h"
#import "NetContant.h"
#import "AlarmData.h"
#import "MainDataBase.h"
#import "GroupViewController.h"

@implementation MessageService

@synthesize operationId;
@synthesize alert;

- (void)main{
    
   

    
    while (![[Globals getInstance] isThreadExit]) {
        
    
       
         NSString *sRet=[[WebData getInstance]getMemberAlarm];
              
        if ([sRet isEqualToString:[[NetContant getInstance] NET_CONNECT_FALSE]]) {
            
            
            
        }else if([sRet isEqualToString:[[NetContant getInstance] NETWORK_RECORD_ISEMPTY]]){
            
          
            
        }else {
            
            @try {
                
               
               NSMutableArray * arrayOfData= [self doNetRet:sRet];
                if([arrayOfData count]!=0)
                    [self performSelectorOnMainThread:@selector(doAlarm:) withObject:[arrayOfData lastObject] waitUntilDone:NO];
        
            }
            @catch (NSException *exception) {
                
                              
            }
            
        }

         [NSThread sleepForTimeInterval:20];
        
    }

  }

-(void)doAlarm:(AlarmData*)alarm{
    

    if(self.alert==nil){
        
        NSString *messge=[NSString stringWithFormat:@"%@\r\n%@",alarm.message,alarm.alarmDate];
       UIAlertView * alertView= [[UIAlertView alloc] initWithTitle:@"消息" message:messge delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];
        
        self.alert=alertView;
        self.alert.delegate=self;
        
    }
    
    if(![alert isVisible]){
        [self.alert show];
    }


}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex==[alertView firstOtherButtonIndex]) {
        
//        GroupViewController *mGroupViewController=[[GroupViewController alloc] initWithNibName:@"GroupViewController" bundle:nil];
//        mGroupViewController.type=type;
//        [self pushViewController:mGroupViewController animated:YES];
//        [mGroupViewController release];
        
    }


}



-(NSMutableArray*)doNetRet:(NSString *)sRet{
    
    NSArray *tables=[sRet componentsSeparatedByString:[[NetContant getInstance]TABLES_SPLIT_FLAG]];
    NSMutableArray *dataList=[NSMutableArray arrayWithCapacity:0] ;
    for (int i=0; i<[tables count]; i++) {
        if(nil!=[tables objectAtIndex:i]){
            NSArray *record=[[tables objectAtIndex:i]componentsSeparatedByString:[[NetContant getInstance] RECORDS_SPLIT_FLAG]];
            if([record count]==2){
                AlarmData *alarmData=[[AlarmData alloc]init];
                alarmData.message=[record objectAtIndex:0];
                alarmData.alarmDate = [record objectAtIndex:1];
                [dataList addObject:alarmData];
                [alarmData release];
            }
        }
    }
    
    [[MainDataBase getInstance] addAlarms:dataList];
 //   [Globals getInstance].sAlarmUpdate=[NSString stringWithFormat:@"%@ %@",[[DateTool getInstance]getNowDate],[[DateTool getInstance]getNowTime]];
    return dataList;
}


-(void)dealloc{

 
    [self.alert release];
    [super dealloc];
}
@end
